package com.hou.serviceuser.controller;

import com.google.common.cache.LoadingCache;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.cache.Cache;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/cache")
public class CacheController {

    @Autowired
    private LoadingCache<String, Object> loadingCache;
    // API注释,方便查看
    @ApiOperation(value = "获取缓存数据",notes = "查询test")
    @GetMapping("/get")
    public Object get(String key){
        try {
            return loadingCache.get(key);
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return "500";
    }

    @ApiOperation(value = "移除缓存数据",notes = "查询test")
    @GetMapping("/remover")
    public void remover(){
        loadingCache.invalidate("name"); //移除单个
        loadingCache.invalidateAll(); //移除全部
        List<String> keys=new ArrayList<>();
        loadingCache.invalidateAll(keys); //批量移除
    }

}
